#############################################
#This file creates Figures OD1 and OD2
#############################################

###Log
my_log <- file("figure_od1od2_log.txt")

sink(my_log, append=T, type="output")
sink(my_log, append=T, type="message")

cat(readChar(rstudioapi::getSourceEditorContext()$path, # Writing currently opened R script to file
             file.info(rstudioapi::getSourceEditorContext()$path)$size))

start_time <- Sys.time()
start_time



library(foreign)
library(magrittr)
library(multcomp)
library(ggstance)
library(socviz)
library(rio)
library(broom)
library(margins)
library(ggstance)
library(tidyverse)

######################
#Process: 
#' Load each experiment; clean it; 
#' combine; run regressions as needed
#####################


############################
#Experiment 1
############################
####Load the data
exp1 <- import("./Data/exp1_cleaned.csv")

####Clean the data as needed
#DV
exp1$support01 <- scales::rescale(exp1$support)

#Treatment
exp1 <- exp1 %>%
  mutate(treat = factor(condition, 
                        levels=c("No Party Cue", 
                                 "Party Cue", 
                                 "Pos Process", 
                                 "Neg Process")))

#Means
means <- exp1 %>%
  group_by(treat) %>%
  summarize(mean = mean(support01, na.rm=T), 
            ssd = sd(support01, na.rm=T), 
            count = n()) %>%
  mutate(se = ssd/sqrt(count), 
         lower = mean - (1.96*se), 
         upper = mean + (1.96*se)) %>%
  filter(!is.na(treat)) %>%
  select(treat, mean, lower, upper) %>%
  mutate(outcome = "Means") %>%
  rename(estimate = mean, 
         term = treat)

models <- tidy(lm(support01 ~ treat, data = exp1)) %>%
  mutate(lower = estimate - (1.96*std.error), 
         upper = estimate + (1.96*std.error)) %>%
  select(term, estimate, lower, upper) %>%
  mutate(outcome = "ATE", 
         term = prefix_strip(term, "treat")) %>%
  filter(term != "(Intercept)") 

exp1_comb <- rbind(means, models)
  

exp1_comb <- exp1_comb %>%
  mutate(term = factor(term, levels=c( "Neg Process",  "Pos Process", "Party Cue","No Party Cue")), 
         outcome = factor(outcome, levels=c("Means", "ATE")))

exp1_comb %>%
  ggplot(aes(x=estimate, y=term)) + 
  geom_pointrangeh(aes(xmin=lower, xmax=upper)) + 
  facet_grid(~outcome, scales="free_x") + 
  geom_text(data=exp1_comb, aes(label=round(estimate,2)),
            nudge_y=0.25) +
  geom_vline(data=filter(exp1_comb, outcome=="ATE"), 
             aes(xintercept=0), linetype="dashed") + 
  labs(x="Estimate", y="Condition") + 
  theme_bw()

ggsave("./Online Appendix D/Figure OD1.png", height=6, width=12)

####Exp 2. 


####Load the data
exp2 <- import("./Data/exp2_cleaned.csv")

####Clean the data as needed
#DV
exp2$support01 <- scales::rescale(exp2$policy_support)

#Treatment Condition(s)
exp2 <- exp2 %>%
  mutate(treat = factor(treat_info, 
                        levels=c(1,2,3,4), 
                        labels=c("No Party Cue", "Party Cue", "Pos Process", "Neg Process")), 
         stereo = factor(exp2$treat_policy_congruence, 
                         levels=c(0,1),
                         labels=c("Counter-Stereotypical", "Stereotypical")))


##Means
exp2_means <- exp2 %>%
  group_by(treat, stereo) %>%
  summarize(mean = mean(support01, na.rm=T), 
            ssd = sd(support01, na.rm=T), 
            count = n()) %>%
  mutate(se = ssd/sqrt(count), 
         lower = mean - (1.96*se), 
         upper = mean + (1.96*se), 
         outcome = "Means") %>%
  filter(!is.na(stereo)) %>%
  rename(estimate = mean) %>%
  select(treat, stereo, estimate, lower, upper, outcome) %>% 
  ungroup

exp2_counter <- tidy(lm(support01 ~ treat, data=exp2, subset=(stereo == "Counter-Stereotypical"))) %>%
  filter(term != "(Intercept)") %>%
  mutate(lower = estimate - (1.96*std.error), 
         upper = estimate + (1.96*std.error), 
         stereo = "Counter-Stereotypical", 
         outcome = "ATE") %>%
  rename(treat = term)  %>%
  mutate(treat = socviz::prefix_strip(treat, "treat")) %>%
  select(treat, estimate, lower, upper, stereo, outcome)

exp2_stereo <- tidy(lm(support01 ~ treat, data=exp2, subset=(stereo == "Stereotypical"))) %>%
  filter(term != "(Intercept)") %>%
  mutate(lower = estimate - (1.96*std.error), 
         upper = estimate + (1.96*std.error), 
         stereo = "Stereotypical", 
         outcome = "ATE") %>%
  rename(treat = term)  %>%
  mutate(treat = socviz::prefix_strip(treat, "treat")) %>%
  select(treat, estimate, lower, upper, stereo, outcome)

exp2_comb <- rbind(exp2_means, exp2_counter, exp2_stereo)

exp2_comb <- exp2_comb %>%
  mutate(outcome = factor(outcome, levels=c("Means", "ATE")), 
         treat = factor(treat, levels=c( "Neg Process",  "Pos Process", "Party Cue","No Party Cue")), 
         stereo = factor(stereo, levels=c("Stereotypical", "Counter-Stereotypical")))

exp2_comb %>%
  ggplot(aes(x=estimate, y=treat)) + 
  geom_pointrangeh(aes(xmin=lower, xmax=upper)) + 
  facet_grid(stereo ~ outcome, scales="free_x") + 
  geom_text(data=exp2_comb, aes(label=round(estimate,2)),
            nudge_y=0.25) +
  geom_vline(data=filter(exp2_comb, outcome=="ATE"), 
             aes(xintercept=0), linetype="dashed") + 
  labs(x="Estimate", y="Condition") + 
  theme_bw()

ggsave("./Online Appendix D/figure_od2.png", height=6, width=12)

end_time <- Sys.time()
end_time

closeAllConnections()

